using System.Collections.Generic;
using Voodoo.Sauce.Core;

namespace Voodoo.Sauce.Internal.Analytics
{
	public static class VoodooFunnelsManager
	{
		private const string TAG = "VOODOO_FUNNELS";

		private const string NONEXISTENT_FUNNEL = "The Funnel you are attempting to track was not set in your VoodooSettings";

		private const string NONEXISTENT_FUNNEL_STEP = "The Funnel Step you are attempting to track was not set in your VoodooSettings";

		private static readonly Dictionary<string, VoodooFunnel> FunnelsDict = new Dictionary<string, VoodooFunnel>();

		private static bool _isInitialized;

		private static void Initialize()
		{
			VoodooFunnelsSettings voodooFunnels = VoodooSettings.Load().VoodooFunnels;
			if (voodooFunnels != null)
			{
				VoodooFunnelSettings[] funnels = voodooFunnels.funnels;
				foreach (VoodooFunnelSettings voodooFunnelSettings in funnels)
				{
					FunnelsDict.Add(voodooFunnelSettings.name, new VoodooFunnel(voodooFunnelSettings));
				}
				_isInitialized = true;
			}
		}

		internal static void Reset()
		{
			_isInitialized = false;
			FunnelsDict.Clear();
		}

		public static void TrackFunnel(string funnelName, string funnelStep)
		{
			if (!_isInitialized)
			{
				Initialize();
			}
			if (!FunnelsDict.ContainsKey(funnelName))
			{
				VoodooLog.LogError(Module.ANALYTICS, "VOODOO_FUNNELS", "The Funnel you are attempting to track was not set in your VoodooSettings");
			}
			else if (!FunnelsDict[funnelName].HasStepName(funnelStep))
			{
				VoodooLog.LogError(Module.ANALYTICS, "VOODOO_FUNNELS", "The Funnel Step you are attempting to track was not set in your VoodooSettings");
			}
		}
	}
}
